// Devin Koepl, Kevin Kemper

#include <stdlib.h>
#include <unistd.h>
#include <gtkmm.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>

#include <cairomm/context.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <ros/ros.h>

#include <fca/ucontroller.h>

#include <fca_controllers/controller.h>
#include <fca_controllers/uspace_kern_shm.h>

#include <drl_library/drl_math.h>

#include <rtai_shm.h>
#include <rtai_nam2num.h>



// GUI objects
Gtk::Window			*window;

Gtk::Notebook		*controller_notebook;

// controller specifics
Gtk::HScale			*motor_torque_hscale;

Gtk::HScale			*motor_position_hscale;
Gtk::HScale			*p_motor_position_hscale;
Gtk::HScale			*d_motor_position_hscale;

Gtk::HScale			*force_hscale;
Gtk::HScale			*pos_hscale;
Gtk::HScale			*tp_hscale;
Gtk::HScale			*p_force_hscale;
Gtk::HScale			*d_force_hscale;
Gtk::HScale			*f_force_hscale;
Gtk::HScale			*p_pos_hscale;
Gtk::HScale			*d_pos_hscale;
Gtk::HScale			*force_trq_lim_hscale;
Gtk::CheckButton	*cut_chkbox;

Gtk::HScale			*trq_amp_hscale;
Gtk::HScale			*trq_freq_hscale;
Gtk::HScale			*trq_off_hscale;
Gtk::HScale			*trq_k1_hscale;
Gtk::HScale			*trq_k2_hscale;
Gtk::HScale			*trq_ke1_hscale;
Gtk::HScale			*trq_ke2_hscale;
Gtk::HScale			*trq_lim_hscale;

Gtk::HScale			*pos_amp_hscale;
Gtk::HScale			*pos_freq_hscale;
Gtk::HScale			*pos_off_hscale;
Gtk::HScale			*pos_k1_hscale;
Gtk::HScale			*pos_k2_hscale;
Gtk::HScale			*pos_ke1_hscale;
Gtk::HScale			*pos_ke2_hscale;

Gtk::HScale			*open_k_hscale;
Gtk::HScale			*open_B_hscale;
Gtk::HScale			*open_I_hscale;
Gtk::HScale			*open_f_hscale;
Gtk::HScale			*open_a_hscale;
Gtk::HScale			*open_lim_hscale;


Gtk::DrawingArea	*drawing_area;

Gtk::ProgressBar	*motor_torque_progress_bar;

Gtk::CheckButton	*log_file_chkbox;
Gtk::Entry			*log_file_name;

Gtk::Button			*restart_button;
Gtk::Button			*enable_button;
Gtk::Button			*disable_button;

Cairo::RefPtr<Cairo::Context> cr;
Gtk::Allocation drawing_allocation;


void print_msg( int );
//void * msg_task( void * );

void log_data_entry( FILE *, int );
void * datalogging_task( void * );


bool poke_controller( void );

void log_chkbox_toggled( void );
void cut_chkbox_toggled( void );

void restart_robot( void );
void enable_motors( void );
void disable_motors( void );
void log_start( void );

void switch_controllers(GtkNotebookPage *, guint);

void draw_leg();
